package com.zhiwei.codesecurity.service.interfaces;

import com.github.pagehelper.PageInfo;
import com.zhiwei.codesecurity.common.base.NullObjectResp;
import com.zhiwei.codesecurity.common.base.ServiceResponse;
import com.zhiwei.codesecurity.common.utils.BeanUtil;
import com.zhiwei.codesecurity.common.utils.ServiceResponseUtil;
import com.zhiwei.codesecurity.entity.Interfaces;
import com.zhiwei.codesecurity.mapper.InterfaceMapper;
import com.zhiwei.codesecurity.mapper.InterfacesDao;
import com.zhiwei.codesecurity.service.interfaces.request.QueryInterfaceReq;
import com.zhiwei.codesecurity.service.interfaces.response.QueryInterfaceDetail;
import com.zhiwei.codesecurity.service.interfaces.response.QueryInterfaceServRlt;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("interfacesService")
public class InterfaceServiceImpl implements IInterfaceService {
    private final static Logger logger = LoggerFactory.getLogger(InterfaceServiceImpl.class);

    @Autowired
    private InterfacesDao interfacesDao;

    @Autowired
    private InterfaceMapper interfaceMapper;

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
    public String AddScanInterface(Interfaces interfaces) {
        logger.info(">>>>>> AddScanInterface start ... ");
        // 请求参数验证
        if (null == interfaces) {
            logger.error("=== 请求参数为空|interfaces= {}", interfaces);
            return null;
        }

        if (0 == interfacesDao.add(interfaces)) {
            logger.error("=======添加接口失败========");
            return null;
        }

        return String.valueOf(interfaces.getInterfaceId());
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
    public ServiceResponse<NullObjectResp> modifyScanInterface(Interfaces interfaces) {
        logger.info(">>>>>> modifyScanInterface start ... ");
        // 请求参数验证
        if (null == interfaces) {
            logger.error("=== 请求参数为空|interfaces= {}", interfaces);
            return ServiceResponseUtil.createServiceResponseForParamError("请求参数为空!");
        }

        Interfaces modifyInterface = interfacesDao.getByPrimaryKey(interfaces.getInterfaceId());
        if (null == modifyInterface) {
            logger.warn("=== 修改接口不存在|userId={}", modifyInterface.getInterfaceId());
            return ServiceResponseUtil.createServiceResponseForParamError("修改接口不存在!");
        }

        modifyInterface.setInterfaceName(interfaces.getInterfaceName());
        modifyInterface.setScanType(interfaces.getScanType());
        modifyInterface.setUrlAddress(interfaces.getUrlAddress());
        modifyInterface.setAccount(interfaces.getAccount());
        modifyInterface.setPassword(interfaces.getPassword());
        modifyInterface.setSecretKey(interfaces.getSecretKey());
        modifyInterface.setCommandTemplate(interfaces.getCommandTemplate());
        modifyInterface.setStatus(interfaces.getStatus());
        modifyInterface.setUpdateTime(new Date());
        modifyInterface.setDataVersion(modifyInterface.getDataVersion() + 1);
        if (0 == interfacesDao.update(modifyInterface)) {
            logger.warn("=== 修改接口失败|userId={}", modifyInterface.getInterfaceId());
            return ServiceResponseUtil.createServiceResponseForParamError("修改接口失败!");
        }
        return ServiceResponseUtil.createServiceResponse();
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
    public ServiceResponse<NullObjectResp> deleteScanInterface(String interfaceId) {
        logger.info(">>>>>> deleteScanInterface start ... ");
        // 请求参数验证
        if (null == interfaceId) {
            logger.error("=== 请求参数为空|interfaceId= {}", interfaceId);
            return ServiceResponseUtil.createServiceResponseForParamError("请求参数为空!");
        }

        if (0 == interfacesDao.deleteByPrimaryKey(interfaceId)) {
            logger.warn("=== 删除接口失败|interfaceId={}", interfaceId);
            return ServiceResponseUtil.createServiceResponseForParamError("删除接口失败!");
        }

        return ServiceResponseUtil.createServiceResponse();
    }

    public ServiceResponse<QueryInterfaceServRlt> queryScanInterfaceList(QueryInterfaceReq queryInterfaceReq) {
        logger.info(">>>>>> queryScanInterfaceList start ... ");
        // 请求参数验证
        if (null == queryInterfaceReq) {
            logger.error("=== 请求参数为空|queryInterfaceReq= {}", queryInterfaceReq);
            return ServiceResponseUtil.createServiceResponseForParamError("请求参数为空!");
        }



        List<Interfaces> interfacesList = interfaceMapper.listQuery(queryInterfaceReq.getPageNum(),
                queryInterfaceReq.getPageSize());
        if (null == interfacesList || 0 == interfacesList.size()) {
            logger.warn("=== 查询接口为空|interfacesList={}", interfacesList);
            return ServiceResponseUtil.createServiceResponse();
        }

        QueryInterfaceServRlt queryInterfaceServRlt = new QueryInterfaceServRlt();
        queryInterfaceServRlt.setDatas(new ArrayList<QueryInterfaceDetail>());
        QueryInterfaceDetail queryInterfaceDetail = null;

        PageInfo page = new PageInfo(interfacesList);
        queryInterfaceServRlt.setPageCount(page.getPageNum());
        queryInterfaceServRlt.setPageNum(page.getPageNum());
        queryInterfaceServRlt.setPageSize(page.getPageSize());
        queryInterfaceServRlt.setTotalCount(page.getTotal());
        for (int i = 0; i < interfacesList.size(); i++) {
            queryInterfaceDetail = new QueryInterfaceDetail();
            BeanUtil.copyPropertiesQuietly(interfacesList.get(i), queryInterfaceDetail);
            queryInterfaceServRlt.getDatas().add(queryInterfaceDetail);
        }

        return ServiceResponseUtil.createServiceResponse(queryInterfaceServRlt);

    }
}
